package com.last1mile.validator;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.last1mile.model.Customer;

@Component("customerValidator")
public class CustomerValidator implements Validator {

    @SuppressWarnings("unchecked")
    @Override
    public boolean supports(Class clazz) {
        // just validate the Customer instances
        return Customer.class.isAssignableFrom(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {

        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "loginId", "required.loginID", "Field name is required.");

        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "loginPwd", "required.loginPassword",
                                                  "Field name is required.");

        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "surname", "required.surname", "Field name is required.");

        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "lastname", "required.lastname", "Field name is required.");

        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "sex", "required.sex", "Field name is required.");

        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email", "required.email", "Field name is required.");
    }

}
